<?php


if(!function_exists('formatResponse')){

    /**
     * 格式化响应数据
     *
     * @param   string|int      $errCode
     * @param   string          $message
     * @param   mixed           $data
     * @return  array
     */
    function formatResponse($errCode = 0, $message = null, $data = null){

        $response = [
            'status' => [
                'errCode' => $errCode,
                'message' => $message
            ],
        ];
        
        if($errCode != 0){
            if(!is_null($message)){
                array_set($response, 'status.message', $message); 
            }
        }

        if(!is_null($data)){
            array_set($response, 'data', $data); 
        }

        return $response;
    }
}

if(!function_exists('formatJsonResponse')){

    /**
     * 格式化响应数据
     *
     * @param   mixed           $data
     * @param   string          $message
     * @param   string|int      $errCode
	 * @return  \Illuminate\Http\JsonResponse
     */
    function formatJsonResponse($data = null, $message = '正常', $errCode = 0){
        return Response::json(formatResponse($errCode, $message, $data));
    }
}

